package
{
	import flash.display.Sprite;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	public class TestFocus extends Sprite
	{
		public function TestFocus()
		{
			super();
			
			var text:TextField = new TextField();
			text.border = true;
			text.selectable = true;
			text.type = TextFieldType.INPUT;
			text.text = "this is sample text";
			this.addChild(text);
			this.stage.focus = text;
			text.setSelection(5, 5);
			
			var btn:Sprite = new Sprite();
			btn.graphics.beginFill(0x888888);
			btn.graphics.lineStyle(1, 0xff0000);
			btn.graphics.drawRect(110, 0, 60, 18);
			btn.graphics.endFill();
			
			btn.addEventListener(MouseEvent.CLICK, onBtnClick);
			
			this.addChild(btn);
			
			trace(text.caretIndex);
			text.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
			text.addEventListener(TextEvent.TEXT_INPUT, onTextInput);
		}
		
		public	function	onBtnClick(evt:MouseEvent):void{
			var text:TextField = this.getChildAt(0) as TextField;
			this.stage.focus = text;
		}
		
		public	function	onFocusIn(evt:FocusEvent):void{
			trace("onFocusIn");
		}
		
		public	function	onTextInput(evt:TextEvent):void{
			var text:TextField = evt.target as TextField;
			if (evt.text == "a" ){
				evt.preventDefault();
			}
		}
	}
}